
/**
 *  \file avdtp_internal.h
 *
 *  Internal header file of AVDTP module. This file contains
 *  the data structures, type definitions, and function declarations that
 *  the AVDTP implementation uses internally.
 */

/*
 *  Copyright (C) 2013. Mindtree Ltd.
 *  All rights reserved.
 */

#ifndef _H_AVDTP_INTERNAL_
#define _H_AVDTP_INTERNAL_

/* ------------------------- Header File Inclusion */
/* EtherMind Common Header Files */
#include "BT_common.h"

/* AVDTP API & Module Header Files */
#include "BT_avdtp_api.h"
#include "avdtp.h"

/* For PSM structure and L2CAP APIs */
#include "l2cap.h"

/* For the Write Task & Bottom Halves */
#include "write_task.h"
#include "BT_bottom_half.h"


/* ------------------------- AVDTP Debug Macros */
#ifndef AVDTP_NO_DEBUG
#define AVDTP_ERR(...)                                BT_debug_error(BT_MODULE_ID_AVDTP, __VA_ARGS__)
#else  /* AVDTP_NO_DEBUG */
#define AVDTP_ERR                                     BT_debug_null
#endif /* AVDTP_NO_DEBUG */

#ifdef AVDTP_DEBUG

#define AVDTP_TRC(...)                                BT_debug_trace(BT_MODULE_ID_AVDTP, __VA_ARGS__)
#define AVDTP_INF(...)                                BT_debug_info(BT_MODULE_ID_AVDTP, __VA_ARGS__)

#define AVDTP_debug_dump_bytes(data, datalen) BT_debug_dump_bytes(BT_MODULE_ID_AVDTP, (data), (datalen))

#else /* AVDTP_DEBUG */

#define AVDTP_TRC                                     BT_debug_null
#define AVDTP_INF                                     BT_debug_null

#define AVDTP_debug_dump_bytes(data, datalen)

#endif /* AVDTP_DEBUG */

#ifdef AVDTP_DATA_DEBUG

#define AVDTP_DATA_TRC(...)                           BT_debug_trace(BT_MODULE_ID_AVDTP, __VA_ARGS__)
#define AVDTP_DATA_INF(...)                           BT_debug_info(BT_MODULE_ID_AVDTP, __VA_ARGS__)

#define AVDTP_debug_dump_bytes(data, datalen) BT_debug_dump_bytes(BT_MODULE_ID_AVDTP, (data), (datalen))

#else  /* AVDTP_DATA_DEBUG */

#define AVDTP_DATA_TRC                                BT_debug_null
#define AVDTP_DATA_INF                                BT_debug_null

#define AVDTP_debug_dump_bytes(data, datalen)

#endif /* AVDTP_DATA_DEBUG */


/* ------------------------- Global Definitions */

/** AVDTP Signalling Packet Types */
#define AVDTP_SIG_PKT_SINGLE                          0x00
#define AVDTP_SIG_PKT_START                           0x04
#define AVDTP_SIG_PKT_CONTINUE                        0x08
#define AVDTP_SIG_PKT_END                             0x0C

/** AVDTP Signalling Message Types */
#define AVDTP_SIG_MSG_COMMAND                         0x00
#ifndef AVDTP_HAVE_GENERAL_REJECT
#define AVDTP_SIG_MSG_INVALID                         0x01
#else
#define AVDTP_SIG_MSG_RESPONSE_GEN_REJECT             0x01
#endif /* AVDTP_HAVE_GENERAL_REJECT */
#define AVDTP_SIG_MSG_RESPONSE_ACCEPT                 0x02
#define AVDTP_SIG_MSG_RESPONSE_REJECT                 0x03

/** AVDTP Signalling Identifiers */
#define AVDTP_SIG_ID_DISCOVER                         0x01
#define AVDTP_SIG_ID_GET_CAPABILITIES                 0x02
#define AVDTP_SIG_ID_SET_CONFIGURATION                0x03
#define AVDTP_SIG_ID_GET_CONFIGURATION                0x04
#define AVDTP_SIG_ID_RECONFIGURE                      0x05
#define AVDTP_SIG_ID_OPEN                             0x06
#define AVDTP_SIG_ID_START                            0x07
#define AVDTP_SIG_ID_CLOSE                            0x08
#define AVDTP_SIG_ID_SUSPEND                          0x09
#define AVDTP_SIG_ID_ABORT                            0x0A
#define AVDTP_SIG_ID_SECURITY_CONTROL                 0x0B

/* AVDTP 1.3 related Signal ID's */
#define AVDTP_SIG_ID_GET_ALL_CAPABILITIES             0x0C
#define AVDTP_SIG_ID_DELAY_REPORT                     0x0D

/** Bitmap values for AVDTP Stream Context State */
#define AVDTP_STATE_IDLE                              0x01
#define AVDTP_STATE_CONFIGURED                        0x02
#define AVDTP_STATE_OPEN                              0x04
#define AVDTP_STATE_STREAMING                         0x08
#define AVDTP_STATE_CLOSING                           0x10
#define AVDTP_STATE_ABORTING                          0x20

/** AVDTP Stream End Point Usage */
#define AVDTP_SEP_IS_NOT_IN_USE                       0x00
#define AVDTP_SEP_IS_IN_USE                           0x02

/** AVDTP SEID Constants */
#define AVDTP_SEID_MIN                                0x01
#define AVDTP_SEID_MAX                                0x3E

/** AVDTP TCID Constants */
#define AVDTP_TCID_QUERY                              0x00
#define AVDTP_TCID_MIN                                0x01
#define AVDTP_TCID_MAX                                0x1E

/** AVDTP TSID Constants */
#define AVDTP_TSID_QUERY                              0x00
#define AVDTP_TSID_MIN                                0x01
#define AVDTP_TSID_MAX                                0x1E

/** AVDTP Service Categories */
#define AVDTP_SERVICE_MEDIA                           0x01
#define AVDTP_SERVICE_REPORTING                       0x02
#define AVDTP_SERVICE_RECOVERY                        0x03
#define AVDTP_SERVICE_CONTENT_PROTECTION              0x04
#define AVDTP_SERVICE_HEADER_COMPRESSION              0x05
#define AVDTP_SERVICE_MULTIPLEXING                    0x06
#define AVDTP_SERVICE_MEDIA_CODEC                     0x07
#define AVDTP_SERVICE_DELAY_REPORTING                 0x08

/** AVDTP Length of Service Catagories */
#define AVDTP_LOSC_MEDIA_TRANSPORT                    0x00
#define AVDTP_LOSC_REPORTING                          0x00
#define AVDTP_LOSC_RECOVERY                           0x03
#define AVDTP_LOSC_CONTENT_PROTECTION                 0x02
#define AVDTP_LOSC_HEADER_COMPRESSION                 0x01
#define AVDTP_LOSC_MULTIPLEXING                       0x03
#define AVDTP_LOSC_MEDIA_CODEC                        0x02
#define AVDTP_LOSC_DELAY_REPORTING                    0x00

/** Bitmap mask for AVDTP LCID States */
#define AVDTP_MASK_CONNECTION_IN_PROGRESS             0x01
#define AVDTP_MASK_CONFIGURATION_IN_PROGRESS          0x02
#define AVDTP_MASK_REMOTE_CONF_COMPLETED              0x04
#define AVDTP_MASK_LOCAL_CONF_COMPLETED               0x08
#define AVDTP_MASK_CONNECTED                          0x10
#define AVDTP_MASK_LOCAL_CONFIGURATION_INITIATED      0x20

/** Bitmap mask for Remote and Local Configuration complete */
#define AVDTP_MASK_LOCAL_REMOTE_CONF_COMPLETED        0x0C
#define AVDTP_MASK_REMOTE_COMPLETE_LOCAL_NOT_COMPLETE 0x04

/** Bitmap masks for Service Catagories */
#define AVDTP_MASK_SERVICE_MEDIA                      0x01
#define AVDTP_MASK_SERVICE_REPORTING                  0x02
#define AVDTP_MASK_SERVICE_RECOVERY                   0x04
#define AVDTP_MASK_SERVICE_MULTIPLEXING               0x08
#define AVDTP_MASK_SERVICE_FRAGMENTATION              0x10
#define AVDTP_MASK_SERVICE_ROHC                       0x20
#define AVDTP_MASK_SERVICE_DELAY_REPORTING            0x40

/** Bitmap mask for Auxiliary state in Stream Context */
#define AVDTP_MASK_SET_CONF_IN_PROGRESS               0x01
#define AVDTP_MASK_OPEN_IN_PROGRESS                   0x02
#define AVDTP_MASK_START_IN_PROGRESS                  0x04
#define AVDTP_MASK_SUSPEND_IN_PROGRESS                0x08
#define AVDTP_MASK_CLOSE_IN_PROGRESS                  0x10
#define AVDTP_MASK_ABORT_IN_PROGRESS                  0x20
#define AVDTP_MASK_RECONF_IN_PROGRESS                 0x40
#define AVDTP_MASK_DELAY_REPORT_IN_PROGRESS           0x80

/** Mask values used for Signalling Header */
#define AVDTP_MASK_SIG_PT                             0x0C
#define AVDTP_MASK_SIG_MT                             0x03
#define AVDTP_SIG_TL_BIT_OFFSET                       4

/** AVDTP Misc Constants */
#define AVDTP_INVALID_LCID_INDEX                      0xFF
#define AVDTP_INVALID_TRANSPORT_INDEX                 0xFF
#define AVDTP_INVALID_SEP_STATE                       0
#define AVDTP_INVALID_USER_OPERATION                  0x00
#define AVDTP_INVALID_TRANSPORT_CID                   0x0000
#define AVDTP_INVALID_TRANSPORT_SESSION               0x00
#define AVDTP_ALLOCATED_TRANSPORT_CID                 0x0001


/**
 *  This is total number of AVDTP signalling packet
 *  handler, each packet handler handles different
 *  type of Siganlling Identifier.
 */
#ifdef AVDTP_HAVE_DELAY_REPORTING
/* Delay reporting feature depends on GET all capabilities */
#define AVDTP_NUM_SIG_PACKET_HANDLERS                 (10 + 3)
#elif (defined AVDTP_HAVE_GET_ALL_CAPABILITIES)
#define AVDTP_NUM_SIG_PACKET_HANDLERS                 (10 + 2)
#else
#define AVDTP_NUM_SIG_PACKET_HANDLERS                 10
#endif /* AVDTP_HAVE_DELAY_REPORTING */

/** Signalling Packet Header Length */
#define AVDTP_RESPONSE_HDR_LEN                        2
#define AVDTP_RECONFIGURE_CMD_HDR_LEN                 3
#define AVDTP_SET_CONFIGURATION_HDR_LEN               4

/** AVDTP Service Category Header Length */
#define AVDTP_SERVICE_CATEGORY_HDR_LEN                2

/**
 *  AVDTP Signalling Command size for single type
 *  packets, including L2CAP Header of 4 Bytes.
 */
#ifndef AVDTP_HAVE_DELAY_REPORTING
#define AVDTP_MAX_COMMAND_SINGLE_SIZE                 7
#else
/* Adding 2 bytes for delay report */
#define AVDTP_MAX_COMMAND_SINGLE_SIZE                 (7 + 2)
#endif /* AVDTP_HAVE_DELAY_REPORTING */

/**
 *  AVDTP Signalling Response size for single type
 *  packets, including L2CAP Header of 4 Bytes.
 */
#define AVDTP_MAX_RESPONSE_SINGLE_SIZE                8


/** Bitmap Mask for AL-PDU Packet Handling */
#define AVDTP_MASK_MULTIPLEXING_FBIT                  0x80
#define AVDTP_MASK_AL_HDR_FBIT                        0x04
#define AVDTP_MASK_AL_HDR_LCODE                       0x03

/** Mask values used for decoding Discover Response */
#define AVDTP_MASK_SEP_IN_USE                         0x02
#define AVDTP_MASK_ACP_SEID                           0xFC
#define AVDTP_MASK_SEP_SOURCE                         0x08
#define AVDTP_MASK_MEDIA_TYPE                         0xF0

/**
 *  To decode packet length in AL-PDU packet
 *  Handling.
 */
#define AVDTP_SET_AL_LENGTH                           0x0100

/** */
#define AVDTP_L2CAP_CID_START                         0x0040


/* GAVDP Timeout Value for AVDTP Commands */
#define AVDTP_GAVDP_COMMAND_TIMEOUT                   3


/* ------------------------- Macros */

/**
 *  AVDTP Dynamic Memory Allocation and Release
 *  macros.
 */
#define avdtp_malloc                                  BT_alloc_mem
#define avdtp_free                                    BT_free_mem


/** Lock the AVDTP global mutex */
#define avdtp_lock()                 BT_MUTEX_LOCK(avdtp_mutex, AVDTP)

/** Unlock the AVDTP global mutex */
#define avdtp_unlock()               BT_MUTEX_UNLOCK(avdtp_mutex, AVDTP)

/** Lock the AVDTP global mutex */
#define avdtp_lock_void()            BT_MUTEX_LOCK_VOID(avdtp_mutex, AVDTP)

/** Unlock the AVDTP global mutex */
#define avdtp_unlock_void()          BT_MUTEX_UNLOCK_VOID(avdtp_mutex, AVDTP)


/** To set function,params and length in the Bottom Half strucure */
#define AVDTP_SET_BH_FUNCTION(bh, function)    ((bh).bh_function = (function))
#define AVDTP_SET_BH_PARAMS(bh, params)        ((bh).bh_params   = (params))
#define AVDTP_SET_BH_PARAMS_LENGTH(bh, length) ((bh).bh_params_len = (length))

/** Validation of AVDTP Contexts */
#define AVDTP_VALIDATE_CONTEXT(context)                      \
    if((context) > AVDTP_MAX_USER_CONTEXTS)                  \
    {                                                        \
        AVDTP_ERR(                                           \
        "[AVDTP Context %d]"                                 \
        "FAILED to Validate Context\n",                      \
        (context));                                          \
        avdtp_unlock_void();                                 \
        return;                                              \
    }

/** To set/get/reset a Bit in AVDTP Bitmaps */
#define AVDTP_SET_BIT(bitmap, mask)     (bitmap) |= (mask)
#define AVDTP_GET_BIT(bitmap, mask)     (((bitmap) & (mask)) == (mask))
#define AVDTP_RESET_BIT(bitmap, mask)   (bitmap) ^= (mask)

/**
 * Macro to extract single byte error code defined in the AVDTP specification,
 * from the corresponding error values defined in BT_error.h header file.
 */
#define AVDTP_SPEC_DEFINED_ERROR_CODE(error) (UCHAR)((error) & 0x00FF)

/** To check if AVDTP Recovery Capability Parameters are valid */
#define AVDTP_IS_RECOVERY_CAP_PARAM_VALID(recovery_type, mrws, mnmp)    \
        (((AVDTP_RECOVERY_TYPE_RFC2733 == recovery_type) &&       \
        ((AVDTP_MIN_MRWS <= mrws) && (AVDTP_MAX_MRWS >= mrws)) && \
        ((AVDTP_MIN_MNMP <= mnmp) && (AVDTP_MAX_MNMP >= mnmp)))   \
        ? BT_TRUE : BT_FALSE)


/* ------------------------- Internal Structures/Data Types */

/* AVDTP Bottom Half Handlers */
typedef void (* AVDTP_BH_HANDLER)
             (
                 /* IN */ void  *    event_data,
                 /* IN */ UINT16     event_datalen
             );

/* AVDTP incoming Signalling Packet Handlers */
typedef void (* AVDTP_SIG_HANDLER)
             (
                 /* IN */ UCHAR   sig_index,
                 /* IN */ UCHAR   tl,
                 /* IN */ UCHAR   message_type,
                 /* IN */ UCHAR * buf,
                 /* IN */ UINT16  buf_len
             );

/**
 *  \struct AVDTP_TS
 *  \brief  This table is used to store information related to the
 *          AVDTP Transport Sessions.
 *
 *  This table is used to store information related to the
 *  AVDTP Transport Sessions. created when Multiplexing is
 *  configured for a Stream.
 *
 *  \note
 *  \li    An entry from this table is allocated when a Stream is being
 *         configured for Multiplexing Service.
 *
 *  \li    An entry from this table is de-allocated (destroyed) when a
 *         Stream, configured for Multiplexing Service, is closed/disconnected.
 *
 *  \li    An entry in this table is free for use if the tsid is set
 *         to AVDTP_INVALID_TRANSPORT_SESSION.
 *
 *  \li    An entry in this table can be uniquely searched using a valid
 *         tsid & sig_index.
 *
 */

#ifdef AVDTP_HAVE_MULTIPLEXING
typedef struct
{
    /** The Transport Session Identifier (TSID) allocated for this session */
    UCHAR tsid;

    /** The Transport Channel Identifier (TCID) allocated for this session */
    UCHAR tcid;

    /** Index of the AVDTP Signaling Entity for this transport session */
    UCHAR sig_index;

    /** Index of the AVDTP Logical Channel Entity for this transport session */
    UCHAR lcid_index;

    /** Index of the AVDTP Stream Context for this transport session */
    UCHAR sc_index;

}AVDTP_TS;
#endif /* AVDTP_HAVE_MULTIPLEXING */


/**
 *  \struct AVDTP_SIGCH
 *  \brief  This table is used to store information related to the
 *          AVDTP Signalling Channel to a peer AVDTP entity.
 *
 *  This table is used to store information related to the
 *  AVDTP Signalling Channel to a peer AVDTP entity. There can be
 *  only one Signalling Channel between a pair of AVDTP entities.
 *
 *  \note
 *  \li    An entry from this table is allocated under the following
 *         conditions:
 *             When Signaling channel establishment request is received
 *             from the application and there exists no Signaling channel
 *             to the specified remote device (AVDTP entity).
 *
 *             When L2CAP connection indication is received from a Bluetooth
 *             Device Address (BD_ADDR) for which there exists no Signaling
 *             channel.
 *
 *  \li    An entry from this table is de-allocated (destroyed) when
 *         the following occurs:
 *             When Signaling channel disconnection request is received
 *             from the application.
 *
 *             When L2CAP disconnection indication is received for a CID
 *             belonging to a Signaling channel to a remote AVDTP entity
 *             (BD_ADDR).
 *
 *  \li    An entry in this table is free for use if the lcid_index is
 *         set to AVDTP_INVALID_LCID_INDEX.
 *
 *  \li    An entry in this table can be uniquely searched using the
 *         Bluetooth Device Address (BD_ADDR) of the remote AVDTP entity.
 *
 */
typedef struct
{

#ifdef AVDTP_HAVE_MULTIPLEXING
    /** Bitmap indicating the allocated & free TCID values. */
    UINT32 tcid_allocated;

    /** Bitmap indicating the allocated & free TSID values. */
    UINT32 tsid_allocated;
#endif /* AVDTP_HAVE_MULTIPLEXING */

    /**
     *  Buffer allocated to store segments of an AVDTP Signalling
     *  messages before it is interpreted.
     */
    UCHAR * reasm_buffer;

    /** Current size of the AVDTP signalling message reassembly buffer. */
    UINT16 reasm_buffer_len;

    /**
     *  Size of AVDTP signalling message expected in the
     *  subsequent segments.
     */
    UINT16 reasm_len_expected;

    /** Number of AVDTP signalling message segment expected */
    UCHAR reasm_num_expected;

    /**
     *  Transaction Label and Message Type of AVDTP signalling packet
     *  being reassembled. The bit map is as given below.
     *  bit 0, bit 1: Message Type.
     *  bit 4, bit 5, bit 6, bit 7: Transcation Label.
     */
    UCHAR reasm_tl_mt;

    /** AVDTP Signalling Identifier of the packet being reassembled. */
    UCHAR reasm_sig_id;

    /**
     *  AVDTP LCID Entity Indexr of the AVDTP signalling message being
     *  reassembled.
     */
    UCHAR lcid_index;

    /**
     *  Index of the LCID Entity, which will be used for the next L2CAP
     *  connection initiated from the peer entity. If the value is
     *  AVDTP_INVALID_LCID_INDEX, it means no L2CAP channel is expected
     *  to open for any stream on this Signaling connection.
     */
    UCHAR lcid_expected;

    /**
     *  Index of the LCID Entity, for which AVDTP is waiting for L2CAP connect
     *  confirmation, that is, this denotes the LCID Entity for which L2CAP
     *  channel establishment has been initiated by AVDTP. If the value is
     *  AVDTP_INVALID_LCID_INDEX, it means no L2CAP channel is expected to
     *  open for any stream on this Signaling connection.
     */
    UCHAR lcid_initiated;

    /** Remote Bluetooth Device Handle for this AVDTP Signalling Channel. */
    DEVICE_HANDLE     handle;

}AVDTP_SIGCH;



/**
 *  \struct AVDTP_STREAM
 *  \brief  This table is used to store information related to an AVDTP
 *          Stream Context created to represent logical streaming
 *          connection between a local and a remote SEP.
 *
 *  This table is used to store information related to an AVDTP
 *  Stream Context created to represent logical streaming
 *  connection between a local and a remote SEP.
 *
 *  \note
 *  \li    An entry from this table is allocated when a SEP is registered
 *         to AVDTP by the application
 *
 *  \li    An entry from this table is de-allocated (destroyed) when a SEP
 *         is de-registered from AVDTP by the application
 *
 *  \li    An entry in this table is free for use if the sep_state is set
 *         to AVDTP_INVALID_SEP_STATE.
 *
 *  \li    An entry in this table can be uniquely searched using the Local
 *         SEID value. Local SEID values are assigned such a way that they
 *         are index to the Stream Context Table. Since the range for SEID
 *         is 0x01-0x3E, correct index to the Stream Context is obtained by
 *         decrementing a given SEID value by one (1).
 *
 *  \li    For every allocated Stream Context Entity, there will be one Stream
 *         End Point Entity allocated and mapped one-to-one. That is, the
 *         Stream End Point Entity for a Stream Context Entity can be addressed
 *         by the same Stream Context index.
 *
 */
typedef struct
{

#ifdef AVDTP_HAVE_REPORTING_SERVICE
    /** Number of Media packets transmitted since the stream was set up. */
    UINT32 num_packets;

    /**
     *  Total Number of Octets in all Media packets transmitted
     *  since the stream was set up.
     */
    UINT32 num_octets;
#endif /* AVDTP_HAVE_REPORTING_SERVICE */

    /**
     *  The callback function pointer - which needs to be called when AVDTP
     *  needs to inform asynchronous events like disconnection or Media packet
     *  reception.
     */
    AVDTP_EVENT_NTF_CB event_ind_cb;

#ifdef AVDTP_HAVE_MULTIPLEXING
    /** Current size of the transport packet reassembly buffer */
    UINT16 reasm_buffer_len;

    /** Expected size of AVDTP transport packet */
    UINT16 reasm_len_expected;
    /**
     *  Buffer allocated to store segments of an AVDTP transport packet
     *  (Media, Recovery or Reporting) before it is interpreted.
     */
    UCHAR *reasm_buffer;
#endif /* AVDTP_HAVE_MULTIPLEXING */

    /** In MTU value for Media transport channel */
    UINT16 in_mtu;


    /**
     *  Bitmap to indicate which AVDTP service configured for this
     *  Stream Context. Each bit signifies a particular service,
     *  as described below:
     *  Bit 0: Media service
     *  Bit 1: Reporting service
     *  Bit 2: Recovery service
     *  Bit 3: Multiplexing service
     *  Bit 4: Fragmentation service
     *  Bit 5: Robust Header Compression Service
     */
    UCHAR service_flag;

#ifdef AVDTP_HAVE_ROHC_SERVICE
    /**
     *  Robust Header Compression settings (BackCh, Media, Recovery)
     *  during Multiplexing (AL).
     */
    UCHAR rohc_conf;
#endif /* AVDTP_HAVE_ROHC_SERVICE */

    /**
     *  This contains the index of the allocated TSID Entity (in case the
     *  Stream is configured for Multiplexing Service), or, Logical Channel
     *  Entity for Media transport service.
     */
    UCHAR media_index;

#ifdef AVDTP_HAVE_RECOVERY_SERVICE
    /**
     *  This contains the index of the allocated TSID Entity (in case the
     *  Stream is configured for Multiplexing Service), or, Logical Channel
     *  Entity for Recovery transport service.
     */
    UCHAR recovery_index;
#endif /* AVDTP_HAVE_RECOVERY_SERVICE */

#ifdef AVDTP_HAVE_REPORTING_SERVICE
    /**
     *  This contains the index of the allocated TSID Entity (in case the
     *  Stream is configured for Multiplexing Service), or, Logical Channel
     *  Entity for Reporting transport service.
     */
    UCHAR reporting_index;
#endif /* AVDTP_HAVE_RECOVERY_SERVICE */

    /** Strean End Point Identifier of the remote SEP. */
    UCHAR remote_seid;

    /** Index to the Signalling Channel Entity for this Stream Context */
    UCHAR sig_index;

    /**
     *  AVDTP State of this Stream Context. This element is a bitmap;
     *  meaning of each bit is given below.
     *  Bit 0: IDLE state
     *  Bit 1: CONFIGURED state
     *  Bit 2: OPEN state
     *  Bit 3: STREAMING state
     *  Bit 4: CLOSING state
     *  Bit 5: ABORTING state
     */
    UCHAR stream_state;

    /**
     *  Auxiliary state information to handle collision of signaling commands.
     *  This element is a bitmap; meaning of each bit is given below.
     *  Bit 0: Set Configuration in progress
     *  Bit 1: Open in progress
     *  Bit 2: Start in progress
     *  Bit 3: Suspend in progress
     *  Bit 4: Close in progress
     *  Bit 5: Abort in progress
     *  Bit 6: Reconfigure in progress
     */
    UCHAR aux_state;

    /**
     *  Buffer containing the Codec Capabilities specific capability
     *  information.
     */
    UCHAR codec_ie[AVDTP_MAX_CODEC_IE_LEN];

    /** Size of actual Codec Capabilities information */
    UCHAR codec_ie_len;

    /**
     *  Buffer containing the Content Protection specific capability
     *  information.
     */
    UCHAR cp_ie[AVDTP_MAX_CP_IE_LEN];

    /** Size of actual Content Protection Capability information. */
    UCHAR cp_ie_len;

}AVDTP_STREAM;


/**
 *  \struct AVDTP_LCID
 *  \brief  This table is used to hold information about an AVDTP transport
 *          channels, which are essentially L2CAP channels.
 *
 *  This table is used to hold information about an AVDTP transport
 *  channels, which are essentially L2CAP channels.
 *
 *  \note
 *  \li    An entry from this table is allocated under the following
 *         conditions:
 *             Whenever a Signaling Channel is allocated (TCID = 0)
 *
 *             During handling of AVDTP Set Configuration Command/Response
 *             (TCID != 0)
 *
 *  \li    An entry from this table is de-allocated (destroyed) when
 *         the following occurs:
 *             Whenever a Signaling Channel is de-allocated (TCID = 0).
 *
 *             When L2CAP Disconnection Confirmation/Indication is received
 *             for a LCID.
 *
 *             When negative response is received for AVDTP Set Configuration
 *             Request (TCID != 0).
 *
 *  \li    An entry in this table is free for use if lcid is set to
 *         AVDTP_INVALID_TRANSPORT_CID. The lcid will be set to
 *         AVDTP_ALLOCATED_TRANSPORT_CID at the time of allocation,
 *         which will be modified later by reception of L2CAP Connection
 *         Indication or Confirmation callbacks.
 *
 *  \li    An entry in this table can be uniquely searched under the
 *         following conditions:
 *         Given a valid value of L2CAP Channel Identifier (LCID).
 *
 *         Given valid values for TCID & remote Bluetooth Device Address
 *         (BD_ADDR).
 *
 *  \li    LCID Entity for TCID value 0x0 is reserved for the AVDTP Signaling
 *         Channel. As long as there exists a Signaling Channel entity for a
 *         BD_ADDR, there will be a LCID Entity for the same BD_ADDR
 *         and TCID = 0.
 *
 */
typedef struct
{
    /** L2CAP Channel ID (LCID) */
    UINT16 lcid;

     /**
     *  The maximum amount of data that can be sent over this L2CAP channel
     *  in one L2CAP packet.
     */
    UINT16 out_mtu;

    /**
     *  The Transport Channel ID (TCID) allocated for this L2CAP channel.
     *  The TCID will be set to Zero if this L2CAP Channel Entity is for the
     *  AVDTP Signalling Channel. Otherwise, if multiplexing is not enabled
     *  on this logical channel it will be set to 0xFF. Otherwise, it will
     *  have a valid value.
     */
    UCHAR tcid;

    /**
     *  State of the L2CAP Channel for this TCID/LCID. This element is
     *  a bitmap; meaning of each bit is given below.
     *  Bit 0: Connection in progress
     *  Bit 1: Configuration in progress
     *  Bit 2: Remote configuration completed
     *  Bit 3: Local configuration completed
     *  Bit 4: Connected
     */
    UCHAR state;

    /**
     *  This parameter identifies the handler of any data received on the
     *  transport channel. It is ALWAYS the index to the Signaling Entity
     *  if TCID is Zero.
     *
     *  If TCID has a valid value, then, if multiplexing is NOT enabled on
     *  this transport channel, it is the index to the Stream Context, which
     *  should handle any received data. Otherwise, it is an index to the
     *  Signaling Channel for the Streams being multiplexed.
     */
    UCHAR handler_index;

#ifdef AVDTP_HAVE_MULTIPLEXING
    /** Number of Media transport sessions being multiplexed on this TCID */
    UCHAR num_media;

#ifdef AVDTP_HAVE_REPORTING_SERVICE
    /** Number of Reporting transport sessions being multiplexed on this TCID */
    UCHAR num_reporting;
#endif /* AVDTP_HAVE_REPORTING_SERVICE */

#ifdef AVDTP_HAVE_RECOVERY_SERVICE
    /** Number of Recovery transport sessions being multiplexed on this TCID */
    UCHAR num_recovery;
#endif /* AVDTP_HAVE_RECOVERY_SERVICE */

#endif /* AVDTP_HAVE_MULTIPLEXING */

}AVDTP_LCID;


/**
 *  \struct AVDTP_CONTEXT
 *  \brief  An AVDTP User Context is created for every AVDTP procedure
 *          initiated by the upper layer application.
 *
 *  An AVDTP User Context is created for every AVDTP procedure
 *  initiated by the upper layer application.
 *
 *  \note
 *  \li    An entry from this table is allocated when an application
 *         makes a request for a AVDTP Signaling transaction
 *
 *  \li    An entry from this table is de-allocated (destroyed) when
 *         response for a Signaling transaction is received and the
 *         procedure is completed
 *
 *  \li    An entry in this table is free for use if operation is
 *         set to AVDTP_INVALID_USER_OPERATION.
 *
 *  \li    An entry in this table can be uniquely searched under
 *         the following conditions:
 *         Using the Transaction Label of the Signaling message
 *
 *         Using the BD_ADDR and the operation
 *         (AVDTP_CONNECT_CNF/AVDTP_DISCONNECT_CNF only)
 *
 *  \li   Index of this table is nothing but the Transaction Label
 *        AVDTP uses to identify a Signaling message (commands or responses).
 *        Since Transaction Label is of only 4 bits, there can be a maximum
 *        of 16 entries in this table.
 *
 */
typedef struct
{
    /** AVDTP Handle received with the API */
    AVDTP_HANDLE handle;

    /** The handle of the GAVDP Timer entity running for this command */
    BT_timer_handle timer_handle;

    /**
     *  Location of the application allocated global/resident memory where
     *  response bytes for this command may be copied if required.
     */
    UCHAR * uarea;

    /** Size of the buffer allocated by the application. */
    UINT16 uarea_len;

    /** Identifies the AVDTP API that has created this context element */
    UCHAR operation;

    /**
     *  Index of the Signalling Entity for which this user context has
     *  been created */
    UCHAR sig_index;

    /**
     *  Remote Bluetooth Device Handle
     */
    DEVICE_HANDLE    device_handle;

}AVDTP_CONTEXT;


/**
 *  \struct AVDTP_CONF_BH_PARAMS
 *  \brief  The BottomHalf parameters for the signalling procedures
 *
 *  The BottomHalf parameters for the signalling procedures
 *
 *  \note
 *
 */
typedef struct
{
    /** The information about configured SEP */
    AVDTP_SEP_CONF sep_conf;

    /** Context identifying command. */
    UCHAR context;

}AVDTP_CONF_BH_PARAMS;


/**
 *  \struct AVDTP_MEDIA_WRITE_BH_PARAMS
 *  \brief  The BottomHalf parameters for the Media write
 *
 *  The BottomHalf parameters for the Media write
 *
 *  \note
 *
 */
typedef struct
{
    /** Packet to transmit */
    UCHAR * packet;

    /** Length of the packet */
    UINT16 packet_len;

    /** The local SEID - to get the stream context */
    UCHAR seid;

}AVDTP_MEDIA_WRITE_BH_PARAMS;


/**
 *  \struct AVDTP_BH
 *  \brief  BottomHalf parameter structure
 *
 *  BottomHalf parameter structure
 *
 *  \note
 *
 */
typedef struct
{
    /** The Bottom half function pointer */
    void (* bh_function)(void *args, UINT16 args_length);

    /** The parameters to the BH function */
    void  * bh_params;

    /** Length of the BH parameters */
    UINT16  bh_params_len;

}AVDTP_BH;


/* ------------------------- Internal Function Declarations */

/* ------------------------- L2CAP Connection and Disconnection Functions */

/*
 *  This is a callback function for the incoming
 *  connect request from the peer device.
 */
API_RESULT avdtp_l2ca_connect_ind
           (
               /* IN */  DEVICE_HANDLE *  handle,
               /* IN */  UINT16           lcid,
               /* IN */  UINT16           psm
           );

/*
 *  This is a callback function for the incoming
 *  connect response for the connect request sent.
 */
API_RESULT avdtp_l2ca_connect_cfm
           (
               /* IN */  DEVICE_HANDLE *  handle,
               /* IN */  UINT16           lcid,
               /* IN */  UINT16           result,
               /* IN */  UINT16           status
           );

/*
 *  This is a callback function for the configuration
 *  indications.
 */
API_RESULT avdtp_l2ca_config_ind
           (
               /* IN */  UINT16                 lcid,
               /* IN */  L2CAP_CONFIG_OPTION *  config_option
           );

/*
 *  This is a callback function for the incoming
 *  configuration confirmations.
 */
API_RESULT avdtp_l2ca_config_cfm
           (
               /* IN */  UINT16                 lcid,
               /* IN */  UINT16                 result,
               /* IN */  L2CAP_CONFIG_OPTION *  config_option
           );

/* To Cleanup AVDTP Entities in case of L2CAP Connection Failure */
void avdtp_l2cap_handle_connection_failure
     (
         /* IN */  UCHAR  lcid_index,
         /* IN */  UCHAR  destroy_flag
     );

/* This function completes the configuration procedure */
void avdtp_l2cap_procedure_complete
     (
         /* IN */  UCHAR  lcid_index
     );

/*
 *  This is a callback function for the L2CAP disconnect
 *  indication notification.
 */
API_RESULT avdtp_l2ca_disconnect_ind
           (
               /* IN */  UINT16  cid
           );

/*
 *  This is a callback function for the L2CAP disconnect
 *  confirmation notification.
 */
API_RESULT avdtp_l2ca_disconnect_cfm
           (
               /* IN */  UINT16  cid,
               /* IN */  UINT16  result
           );
/*
 *  This is a callback function registered with L2CAP for the incoming
 *  AVDTP data reception.
 */
API_RESULT avdtp_l2ca_data_read
           (
               /* IN */  UINT16  cid,
               /* IN */  UCHAR * data,
               /* IN */  UINT16  datalen
           );

/*
 *  This function handles the incoming AVDTP Accept Response.
 */
void avdtp_response_accept_handler
     (
         /* IN */  UCHAR    tl,
         /* IN */  UCHAR    sig_id,
         /* IN */  UCHAR *  buf,
         /* IN */  UINT16   buflen
     );

/*
 *  This function handles the incoming AVDTP Reject Response.
 */
void avdtp_response_reject_handler
     (
         /* IN */  UCHAR    tl,
         /* IN */  UCHAR    sig_id,
         /* IN */  UCHAR *  buf,
         /* IN */  UINT16   buflen
     );


/* ------------------------- AVDTP Utility Functions */

/* This function creates the Signalling entity */
API_RESULT avdtp_create_sig_entity
           (
               /* IN */   UCHAR            * bd_addr,
               /* IN */   DEVICE_HANDLE    * handle,
               /* OUT */  UCHAR            * sig_index
           );

/* This function destroys the Signalling Entity */
void avdtp_destroy_sig_entity
     (
         /* IN */  UCHAR  sig_index
     );

/*
 *  This function searches the AVDTP Signalling Entity for a remote
 *  AVDTP Entity.
 */
API_RESULT avdtp_search_sig_entity
           (
               /* IN */   DEVICE_HANDLE    * handle,
               /* OUT */  UCHAR            * sig_index
           );


/* This function resets reassembly buffer and releases the memory */
void avdtp_reset_sig_reasm_buffer
     (
         /* IN */  UCHAR  sig_index
     );

/*
 *  This function searches User Context based on operation to a
 *  remote AVDTP Entity.
 */
API_RESULT avdtp_search_context
           (
               /* IN */   UCHAR   sig_index,
               /* IN */   UCHAR   operation,
               /* OUT */  UCHAR * context
           );

/* This function signals the AVDTP upper layer */
void avdtp_signal_context
     (
         /* IN */  UCHAR       context,
         /* IN */  API_RESULT  result
     );

/* This function searches the LCID Entity based on CID */
API_RESULT avdtp_search_lcid_entity
           (
               /* IN */   UINT16  cid,
               /* OUT */  UCHAR * lcid_index
           );

/* This function searches LCID Entity based on the TCID and sig_index */
API_RESULT avdtp_search_lcid_entity_tcid
           (
               /* IN */   UCHAR   sig_index,
               /* IN */   UCHAR   tcid,
               /* OUT */  UCHAR * lcid_index
           );

/* This function creates the LCID Entity */
API_RESULT avdtp_create_lcid_entity
           (
               /* IN */   UCHAR   tcid,
               /* IN */   UCHAR   handler_index,
               /* OUT */  UCHAR * lcid_index
           );

/* This function destroys the LCID Entity Set Configuration packet */
void avdtp_destroy_lcid_entity
     (
         /* IN */  UCHAR  lcid_index
     );

/*
 *  This function searches for Stream Context in "Open in Progress"
 *  State for a given Signaling Entity, when Multiplexing is configured.
 */
API_RESULT avdtp_search_stream_context_in_open
           (
               /* IN */   UCHAR    sig_index,
               /* OUT */  UCHAR *  sc_index
           );

/* This function handles the incoming signalling packet handlers */
void avdtp_handle_signalling_packet
     (
         /* IN */  UCHAR   sig_index,
         /* IN */  UCHAR * data,
         /* IN */  UINT16  datalen
     );


/*
 *  This function does the common functions before enqueueing
 *  the bh function.
 */
API_RESULT avdtp_common_api_handler
           (
               /* IN */  UCHAR          operation,
               /* IN */  UCHAR *        context,
               /* IN */  AVDTP_HANDLE * handle,
               /* IN */  AVDTP_BH *     bh_params,
               /* IN */  void *         uarea,
               /* IN */  UINT16         uarea_len
           );

/*
 *  Common Incoming Command Handler for the following AVDTP Signalling Commands:
 *  - AVDTP Discover Command
 */
void avdtp_command_handler_set_1
     (
         /* IN */  UCHAR    sig_index,
         /* IN */  UCHAR    sig_id,
         /* IN */  UCHAR    tl,
         /* IN */  UCHAR  * buf,
         /* IN */  UINT16   buf_len
     );

/*
 *  Common Incoming Command Handler for the following AVDTP Signalling Commands:
 *  - AVDTP Get Capabilities  Command
 *  - AVDTP Get Configuration Command
 */
void avdtp_command_handler_set_2
     (
         /* IN */  UCHAR    sig_index,
         /* IN */  UCHAR    sig_id,
         /* IN */  UCHAR    tl,
         /* IN */  UCHAR  * buf,
         /* IN */  UINT16   buf_len
     );

/*
 *  Common Incoming Command Handler for the following AVDTP Signalling Commands:
 *  - AVDTP Set Configuration Command
 *  - AVDTP Reconfiguration   Command
 */
void avdtp_command_handler_set_3
     (
         /* IN */  UCHAR    sig_index,
         /* IN */  UCHAR    sig_id,
         /* IN */  UCHAR    tl,
         /* IN */  UCHAR  * buf,
         /* IN */  UINT16   buf_len
     );

/*
 *  Common Incoming Command Handler for the following AVDTP Signalling Commands:
 *  - AVDTP Open  Command
 *  - AVDTP Close Command
 *  - AVDTP Abort Command
 */
void avdtp_command_handler_set_4
     (
         /* IN */  UCHAR    sig_index,
         /* IN */  UCHAR    sig_id,
         /* IN */  UCHAR    tl,
         /* IN */  UCHAR  * buf,
         /* IN */  UINT16   buf_len
     );

/*
 *  Common Incoming Command Handler for the following AVDTP Signalling Commands:
 *  - AVDTP Start   Command
 *  - AVDTP Suspend Command
 */
void avdtp_command_handler_set_5
     (
         /* IN */  UCHAR    sig_index,
         /* IN */  UCHAR    sig_id,
         /* IN */  UCHAR    tl,
         /* IN */  UCHAR  * buf,
         /* IN */  UINT16   buf_len
     );

#ifdef AVDTP_HAVE_DELAY_REPORTING
/*
 * Handler for Incoming AVDTP Delay Reporting Signalling Command:
 */
void avdtp_command_handler_set_6
     (
        /* IN */ UCHAR    sig_index,
        /* IN */ UCHAR    sig_id,
        /* IN */ UCHAR    tl,
        /* IN */ UCHAR  * buf,
        /* IN */ UINT16   buf_len
     );
#endif /* AVDTP_HAVE_DELAY_REPORTING */

/* This function handles incoming Adaptation Layer Packets */
void avdtp_handle_adaptation_layer_packets
     (
         /* IN */  UCHAR   sig_index,
         /* IN */  UCHAR * data,
         /* IN */  UINT16  data_len
     );

/*
 *  This function creates the transport channels required during Set
 *  Configuration Procedure.
 */
API_RESULT avdtp_create_transport_channels
           (
               /* IN */  UCHAR   sc_index,
               /* IN */  UCHAR   service_flag,
               /* IN */  UCHAR * tsid_tcid
           );

/*
 *  This function creates TSID Entity for multiplxed Transport Sessions.
 *  This function also creates LCID Entity if required.
 */
API_RESULT avdtp_create_transport_session
           (
               /* IN */  UCHAR  sc_index,
               /* IN */  UCHAR  sig_index,
               /* IN */  UCHAR  service_type,
               /* IN */  UCHAR  tsid,
               /* IN */  UCHAR  tcid
           );

/* This function searches for free TSID Entity */
API_RESULT avdtp_create_tsid_entity
           (
               /* OUT */ UCHAR  *tsid_index
           );

/* This function searches for free TSID/TCID value from TSID/TCID bitmap. */
API_RESULT avdtp_allocate_tsid_tcid
           (
               /* IN */   UCHAR   bitmap,
               /* OUT */  UCHAR * tsid_tcid
           );

/* This function destroys Transport Channels for a Stream Context Entity */
void avdtp_destroy_transport_channels
     (
         /* IN */  UCHAR  sc_index,
         /* IN */  UCHAR  service_flag
     );

/*
 *  This function deallocates TSID Entity and TSID, TCID bitmaps,
 *  if required.
 */
void avdtp_dealloc_tcid_tsid
     (
         /* IN */  UCHAR  serive_type,
         /* IN */  UCHAR  tsid_index
     );

/*
 *  This funcion searches for the LCID Entity, which will be used for the
 *  next L2CAP connection initiated from the peer Entity.
 */
void avdtp_get_next_lcid
     (
         /* IN */   UCHAR   sc_index,
         /* OUT */  UCHAR * lcid_next
     );

/*
 *  This function validates Multiplexing Service Parameters received in
 *  Set Configuration packet.
 */
API_RESULT avdtp_validate_mux_params
           (
               /* IN */  UCHAR * buffer,
               /* IN */  UCHAR   service_flag,
               /* IN */  UCHAR   sig_index,
               /* IN */  UCHAR * tsid_tcid
           );

/*
 *  This function is used to send an AVDTP Signalling Command
 *  whose size is less than BT_STATIC_DATA_SIZE.
 */
API_RESULT avdtp_send_command_single
           (
               /* IN */  UCHAR  context,
               /* IN */  UCHAR  lcid_index,
               /* IN */  UCHAR  sig_id
           );

/*
 *  This function is used to send Accept Response to an AVDTP Signalling
 *  Command.
 */
API_RESULT avdtp_send_response_single
           (
               /* IN */  UCHAR  lcid_index,
               /* IN */  UCHAR  tl,
               /* IN */  UCHAR  sig_id,
               /* IN */  UCHAR  error_info,
               /* IN */  UCHAR  error_code
           );

/*
 *  This function is used to send an AVDTP Signalling Message when
 *  service capability description is part of the AVDTP payload.
 */
API_RESULT avdtp_send_service_capability
           (
               /* IN */  UCHAR             tl,
               /* IN */  UCHAR             sig_id,
               /* IN */  UCHAR             serv_flag,
               /* IN */  AVDTP_SEP_CONF *  sep_conf,
               /* IN */  UCHAR             sc_index,
               /* IN */  UCHAR             sig_index
           );

#ifdef HAVE_AVDTP_SEND_FRAGMENTED_SIG_MSG
/**
 * This function is used to send AVDTP signalling message in fragments
 * when message length is greater the Out MTU of the Signalling Channel.
 */
API_RESULT  avdtp_send_sig_ch_data_in_fragments
            (
                /* IN */ UCHAR     lc_index,
                /* IN */ UCHAR     msg_type,
                /* IN */ UCHAR     tl,
                /* IN */ UCHAR    *data,
                /* IN */ UINT16    data_len
            );
#endif /* HAVE_AVDTP_SEND_FRAGMENTED_SIG_MSG */

/* This function resets the Stream Context Entity */
void avdtp_reset_stream_context
     (
         /* IN */ UCHAR  sc_index
     );

/* This function resets the Stream Transport Indexs */
void avdtp_reset_stream_transport_index
     (
         /* IN */  UCHAR  sc_index,
         /* IN */  UCHAR  transport_index
     );


/*
 *  This function finds if channels needs to be released
 *  for a Stream Context Entity.
 */
API_RESULT avdtp_transport_channels_to_be_released
           (
               /* IN */  UCHAR  sc_index
           );

/* This function handles GAVDP Timeout for AVDTP Signaling Commands */
void avdtp_command_timeout_handler
     (
         /* IN */  void *  args,
         /* IN */  UINT16  args_len
     );

/* To findout the InMTU to be used during L2CAP Channel establishment */
void avdtp_derive_in_mtu
     (
         /* IN */  UCHAR     lcid_index,
         /* OUT */ UINT16 *  mtu
     );

/* This function changes AVDTP Stream Context State */
#ifdef AVDTP_DEBUG

    void avdtp_debug_event_ntf
         (
             /* IN */  UCHAR   context,
             /* IN */  UINT16  result
         );

    void avdtp_change_sc_state
         (
             /* IN */  UCHAR  sc_index,
             /* IN */  UCHAR  new_state
         );

    void avdtp_debug_req
         (
             /* IN */  UCHAR context
         );

    void avdtp_debug_response
         (
             /* IN */  UCHAR  sig_index,
             /* IN */  UCHAR  sig_id,
             /* IN */  UCHAR  error_code
         );

    void avdtp_debug_send_cmd
         (
             /* IN */  UCHAR sig_index,
             /* IN */  UCHAR sig_id
         );

    void avdtp_debug_command
         (
             /* IN */  UCHAR  sig_index,
             /* IN */  UCHAR  sig_id
         );

    void avdtp_debug_event_ind
         (
             /* IN */  UCHAR  sc_index,
             /* IN */  UCHAR  sig_id
         );
    void avdtp_debug_bh_user_context
         (
             /* IN */  UCHAR context
         );


#else  /* AVDTP_DEBUG */

    #define avdtp_debug_event_ntf(a, b)

    #define avdtp_change_sc_state(sc_index, new_state) \
            avdtp_streams[(sc_index)].stream_state = (new_state);

    #define avdtp_debug_req(a)
    #define avdtp_debug_send_cmd(a, b)
    #define avdtp_debug_command(a, b)
    #define avdtp_debug_response(a ,b, c)
    #define avdtp_debug_event_ind(a, b)
    #define avdtp_debug_bh_user_context(a)


#endif /* AVDTP_DEBUG */


#endif /* _H_AVDTP_INTERNAL_ */


